﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Sc.Web.Controllers
{
    public class CarInfoController : BaseController
    {
        /// <summary>
        /// 车辆服务层
        /// </summary>
        private readonly CarInfoService service = new CarInfoService();
        /// <summary>
        /// 车辆首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewData["olist"] = CommService.GetOrgzByUserPer(UserLogin.UserId, 1);
            ViewData["type"] = new BaseItemService().GetListByType(5);
            return View();
        }
        /// <summary>
        /// 获取车辆列表
        /// </summary>
        /// <returns></returns>
        public JsonResult GetPageData()
        {
            var orgz = Convert.ToInt32(Request.Params["orgz"]);
            var carNo = Request.Params["carNo"];
            if (orgz == 0)
                orgz = UserLogin.OrgzNo;
            var data = service.GetList(UserLogin.OrgzNo, 0, carNo, 0);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取车辆列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页显示数量</param>
        /// <returns></returns>
        public JsonResult GetPageDataByPage(int pageIndex, int pageSize)
        {
            int state = Convert.ToInt32(Request.Params["state"]);
            string carNo = Request.Params["carNo"];
            var orgz = Convert.ToInt32(Request.Params["orgz"]);
            var carType = Convert.ToInt32(Request.Params["carType"]);

            if (orgz == 0)
                orgz = UserLogin.OrgzNo;
            var data = service.GetList(orgz, pageSize, pageIndex, state, carNo, carType);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取未派出车辆
        /// </summary>
        /// <returns></returns>
        public JsonResult GetNoSendCar(int cartype)
        {
            var data = service.GetList(UserLogin.OrgzNo, 1, string.Empty, cartype);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 创建车辆基本信息
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateCarInfo()
        {
            ViewData["type"] = new BaseItemService().GetListByType(5);
            return View();
        }
        /// <summary>
        /// 保存车辆基本信息
        /// </summary>
        /// <param name="ent">页面数据</param>
        /// <returns></returns>
        public int SaveCarInfo(CarInfo ent)
        {
            if (ent.Id == 0)
            {
                var path = FileUpload();
                ent.ImageUrl = path;
                ent.OrgzNo = UserLogin.OrgzNo;
                return service.Add(ent);
            }
            else
            {
                var file = Request.Files[0];
                if (file.FileName.Length > 0)
                {
                    var path = FileUpload();
                    ent.ImageUrl = path;
                }
                ent.OrgzNo = UserLogin.OrgzNo;
                return service.Update(ent) ? 1 : 0;
            }
        }
        /// <summary>
        /// 获取驾驶员
        /// </summary>
        /// <returns></returns>
        public JsonResult GetDriver()
        {
            string orgzNo = Request.Params["orgz"];
            string uName = Request.Params["userName"];
            int orgz = UserLogin.OrgzNo;
            if (!string.IsNullOrEmpty(orgzNo))
            {
                orgz = Convert.ToInt32(orgzNo);
            }
            var data = service.GetDriver(orgz, uName);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除车辆信息
        /// </summary>
        /// <param name="ids">车辆ID</param>
        /// <returns></returns>
        public int DeleteCarInfo(string ids)
        {
            var temp = ids.Remove(ids.LastIndexOf(','));
            return service.DeleteList(temp) ? 1 : 0;
        }
        /// <summary>
        /// 修改车辆基本信息
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateCarInfo()
        {
            var id = Convert.ToInt32(Request.Params["id"]);
            ViewData["ent"] = service.GetCarInfoById(id);
            ViewData["type"] = new BaseItemService().GetListByType(5);
            return View();
        }
        /// <summary>
        /// 查询车辆基本信息
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowCarInfo()
        {
            var id = Convert.ToInt32(Request.Params["id"]);
            ViewData["ent"] = service.GetCarInfoById(id);
            ViewData["type"] = new BaseItemService().GetListByType(5);
            return View();
        }
        /// <summary>
        /// 车辆选择
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectCarInfo()
        {
            ViewData["olist"] = CommService.GetOrgzByUserPer(UserLogin.UserId, 1);
            return View();
        }
        /// <summary>
        /// 选择司机
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectDriver()
        {
            ViewData["olist"] = CommService.GetOrgzByUserPer(UserLogin.UserId, 1);
            ViewData["sigle"] = Request.Params["r"];
            return View();
        }

        public ActionResult SelectUser()
        {
            ViewData["olist"] = CommService.GetOrgzByUserPer(UserLogin.UserId, 1);
            ViewData["sigle"] = Request.Params["r"];
            return View();
        }

        /// <summary>
        /// 获取驾驶员
        /// </summary>
        /// <returns></returns>
        public string GetUser()
        {
            string orgzNo = Request.Params["orgz"];
            var data = service.GetUsers(Convert.ToInt32(orgzNo));
            return data;
        }

        /// <summary>
        /// 得到车辆位置信息
        /// </summary>
        /// <param name="carno"></param>
        /// <returns></returns>
        public string GetCarSite(string carno)
        {
            return service.GetGpsSite(carno);
        }

        /// <summary>
        /// 车辆历史轨迹
        /// </summary>
        /// <returns></returns>
        public JsonResult GetTrackPic(String id)
        {
            SendCarRecordService record = new SendCarRecordService();
            var temp = record.GetSendCarRecordTemp(int.Parse(id));
            string data = service.GetGpsSitelong(temp.CarNo, temp.StartTime, temp.BackTime);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 车辆历史轨迹
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowTrack()
        {
            string id = Request.Params["id"];
            ViewData["id"] = id;
            return View();
        }

        /// <summary>
        /// 用车申请
        /// </summary>
        /// <returns></returns>
        public ActionResult UseCar()
        {
            return View();
        }

        /// <summary>
        /// 保存用车申请
        /// </summary>
        /// <param name="ent"></param>
        /// <returns></returns>
        public int SaveUseCar(Car_UseCarApplication ent)
        {
            Car_UseCarApplicationService uca = new Car_UseCarApplicationService();
            ent.UserId = UserLogin.UserId;
            ent.OrgzNo = UserLogin.OrgzNo;
            if (ent.IsApproal)
                return uca.SaveDataForApp(ent);
            return uca.SaveData(ent);
        }
    }
}